package com.hamster.crmpre.service.impl;

import com.hamster.crmpre.config.LoginInterceptor;
import com.hamster.crmpre.mapper.HandOverAssistantMapper;
import com.hamster.crmpre.mapper.NoticeMapper;
import com.hamster.crmpre.mapper.PersonalMessageMapper;
import com.hamster.crmpre.pojo.HandOverAssistant;
import com.hamster.crmpre.pojo.Notice;
import com.hamster.crmpre.pojo.PersonalMessage;
import com.hamster.crmpre.service.CustomerAssistantService;
import com.hamster.crmpre.service.MessageQueryService;
import com.hamster.crmpre.service.PhoneUserService;
import com.sun.xml.bind.v2.model.core.ID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

/**
 * @author ：Lizhipeng
 * @date ：Created in 2019/12/30 15:22
 * @description：转交助教实现
 * @modified By：
 * @version: 1.0
 */
@Service
public class CustomerAssistantServiceImpl implements CustomerAssistantService {
    @Autowired
    private PhoneUserService phoneUserService;
    @Autowired
    private PersonalMessageMapper personalMessageMapper;
    @Autowired
    private HandOverAssistantMapper handOverAssistantMapper;
    @Autowired
    private MessageQueryService messageQueryService;
    @Autowired
    private NoticeMapper noticeMapper;

    /**
     * 转交助教接收按钮实现
     * @param map
     */
    @Override
    @Transactional
    public void receive(Map<String, String> map) {
        messageQueryService.setRead(map.get("id"));
        //获取要操作的消息对象
        PersonalMessage personalMessage = messageQueryService.get(map.get("id"));
        //获取要操作的对象的详细消息的对象
        String messageLink = personalMessage.getMessageLink();

        //更改消息的状态
        HandOverAssistant handOverAssistant = handOverAssistantMapper.selectById(messageLink);
        handOverAssistant.setReceiveFlag("1");
        handOverAssistantMapper.updateById(handOverAssistant);
        //创建个人消息中心的对象
        //通知财务一声
        //通知财务一声
        Notice notice = new Notice();
        notice.setContent("员工："+personalMessage.getSenderName()+"转交助教给"+"员工："+personalMessage.getReceiverName()
        +"  客户："+handOverAssistant.getCustomer()+"  备注 : "+handOverAssistant.getRemark()
        );
        noticeMapper.insert(notice);

        PersonalMessage newPersonalMessage = new PersonalMessage();
        newPersonalMessage.setMessageType("通知消息");
        newPersonalMessage.setSenderId("系统");
        newPersonalMessage.setSenderName("系统");
        newPersonalMessage.setMessageLink(notice.getId());
        newPersonalMessage.setCreateTime(new Date());
        newPersonalMessage.setState("0");//消息状态 0未读
        newPersonalMessage.setReceiverId("财务部");
        newPersonalMessage.setReceiverName("财务部");
        newPersonalMessage.setDepartment("财务部");
        personalMessageMapper.insert(newPersonalMessage);

    }
}
